/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JChangeRateDialog.java
 *
 * Created on 2009-8-12, 10:35:16
 */

package com.openbravo.pos.sales;

import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.base.AppLocal;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingUtilities;

/**
 *
 * @author Administrator
 */
public class JChangeRateDialog extends AbstractKeyBoardJDialog {

    Double changeRate;
    boolean isOk;
    /** Creates new form JChangeRateDialog */
    public JChangeRateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
    }

     /** Creates new form JProductLineKeyBoardEdit */
    private  JChangeRateDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        m_jKeys = new com.openbravo.editor.JEditorKeys();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jPriceRant = new com.openbravo.editor.JEditorIntegerPositive();
        jPanel1 = new javax.swing.JPanel();
        m_jButtonOK = new javax.swing.JButton();
        m_jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(389, 240));
        setResizable(false);

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.PAGE_AXIS));
        jPanel4.add(m_jKeys);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        jLabel8.setText(AppLocal.getIntString("label.priceRate")); // NOI18N
        jLabel8.setPreferredSize(new java.awt.Dimension(80, 25));
        jPanel3.add(jLabel8);
        jPanel3.add(jPriceRant);

        jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel4.add(jPanel2);

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        m_jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png"))); // NOI18N
        m_jButtonOK.setText(AppLocal.getIntString("Button.OK")); // NOI18N
        m_jButtonOK.setFocusPainted(false);
        m_jButtonOK.setFocusable(false);
        m_jButtonOK.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonOK.setRequestFocusEnabled(false);
        m_jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonOKActionPerformed(evt);
            }
        });
        jPanel1.add(m_jButtonOK);

        m_jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
        m_jButtonCancel.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
        m_jButtonCancel.setFocusPainted(false);
        m_jButtonCancel.setFocusable(false);
        m_jButtonCancel.setMargin(new java.awt.Insets(8, 16, 8, 16));
        m_jButtonCancel.setRequestFocusEnabled(false);
        m_jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_jButtonCancelActionPerformed(evt);
            }
        });
        jPanel1.add(m_jButtonCancel);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-230)/2, (screenSize.height-356)/2, 230, 356);
    }// </editor-fold>//GEN-END:initComponents

    private void m_jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonOKActionPerformed
        changeRate=jPriceRant.getDoubleValue()/100;
        isOk=true;
        dispose();
    }//GEN-LAST:event_m_jButtonOKActionPerformed

    private void m_jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jButtonCancelActionPerformed
       dispose();
    }//GEN-LAST:event_m_jButtonCancelActionPerformed


    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private com.openbravo.editor.JEditorIntegerPositive jPriceRant;
    private javax.swing.JButton m_jButtonCancel;
    private javax.swing.JButton m_jButtonOK;
    private com.openbravo.editor.JEditorKeys m_jKeys;
    // End of variables declaration//GEN-END:variables

    @Override
    protected void enterKeyProc() {
        if(m_jButtonOK.isEnabled())
            m_jButtonOKActionPerformed(null);
    }

    @Override
    protected void escapeKeyProc() {
        dispose();
    }

    @Override
    protected void upKeyProc() {
    }

    @Override
    protected void downKeyProc() {
        
    }

    public Double getChangeRate() {
        return changeRate;
    }

    public boolean isOk() {
        return isOk;
    }

    public static JChangeRateDialog getDialog(Component parent, Double changeRate){
        Window window = SwingUtilities.getWindowAncestor(parent);
        JChangeRateDialog myMsg;
        if (window instanceof Frame) {
            myMsg = new JChangeRateDialog((Frame) window, true);
        } else {
            myMsg = new JChangeRateDialog((Dialog) window, true);
        }
        myMsg.init(changeRate);
        myMsg.applyComponentOrientation(parent.getComponentOrientation());
        return myMsg;
    }

    private void init(Double rate) {
        initComponents();
        isOk=false;
        changeRate=rate*100;
        jPriceRant.setDoubleValue(changeRate);
        jPriceRant.addPropertyChangeListener("Edition", new RecalculatePriceRant());
        jPriceRant.addEditorKeys(m_jKeys);
        jPriceRant.activate();
        getRootPane().setDefaultButton(m_jButtonOK);
    }

     private class RecalculatePriceRant implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            Double value = jPriceRant.getDoubleValue();
            if (value == null||value<0||value>100) {
                m_jButtonOK.setEnabled(false);
            } else {
                m_jButtonOK.setEnabled(true);
            }
        }
    }

}
